package com.cykj.service.Impl;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.AppointmentTimeMapper;
import com.cykj.pojo.PsAppointmentTime;
import com.cykj.pojo.PsTime;
import com.cykj.service.AppointmentService;
import com.cykj.service.AppointmentTimeService;
import com.cykj.vo.AppointmentTimeVo;
import com.cykj.vo.UserAssessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-02-11 13:36
 * @Description: 获取时间
 * @Version: 1.0
 */
@Service
public class AppointmentTimeServiceImpl implements AppointmentTimeService {
    @Autowired
    private AppointmentTimeMapper appointmentTimeMapper;

    /**
     * 根据传入的查询条件搜索预约时间
     * @param vo
     * @return
     */
    @Override
    public ResponseDto search(AppointmentTimeVo vo) {
        List<PsAppointmentTime> appointmentTimeList = appointmentTimeMapper.select(vo);
        if (!appointmentTimeList.isEmpty()) {
            return ResponseDto.success(appointmentTimeList);
        } else {
            return ResponseDto.error("查询失败");
        }
    }

    /**
     * 插入预约时间信息，该操作具有事务性，若出现异常会回滚
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto insert(AppointmentTimeVo vo) {
        try {
            appointmentTimeMapper.delete(vo);
            for (int i = 0; i < vo.getAppointmentTime().size(); i++) {
                vo.setTimeId(vo.getAppointmentTime().get(i));
                appointmentTimeMapper.insert(vo);
            }
            return ResponseDto.success("成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 搜索所有的预约时间
     * @return
     */
    @Override
    public ResponseDto searchAll() {
        List<PsTime> timeList = appointmentTimeMapper.selectAll();
        if (!timeList.isEmpty()) {
            return ResponseDto.success(timeList);
        } else {
            return ResponseDto.error("查询失败");
        }
    }

}

